package GUILayer;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import CtrLayer.EventCtr;
import ModelLayer.Employee;
import ModelLayer.Event;

/**
 * EventGUI
 * this class creates the eventGUIPanel which contains a tabbed pane with panels with info on various events.
 * 31.05.2012
 * @author Lindgren
 * @Version 0.1
 */

public class EventGUI  {

	private Employee employee;
	private JPanel eventGUIPanel;
	private JTextArea textAreaTodaysEvents;
	private JTextArea textAreaUpcomingEvents;
	private JTextArea textAreaRecentEvents;
	private JTabbedPane tabbedPaneEvents;

	/**
	 * Create the eventGUI.
	 */
	public EventGUI(Employee employee) {
		this.employee = employee;
		initialize();
		System.out.println("EventGUI initialized!");
	}

	/**
	 * initialize the eventGUI.
	 */
	private void initialize() {

		System.out.println("EventGUI initializing");

		eventGUIPanel = new JPanel();
		eventGUIPanel.setToolTipText("eventGUIPanel");

		eventGUIPanel.setName(employee.getLogin());
		MainFrame.getEventGUIPanels().put(employee.getLogin(), eventGUIPanel);
		System.out.println("EventGUI eventGUIPanel mapped: "+MainFrame.getEventGUIPanels().get(employee.getLogin()));
		eventGUIPanel.setLayout(new GridLayout(0, 1, 0, 0));

		tabbedPaneEvents = new JTabbedPane(JTabbedPane.RIGHT);
		eventGUIPanel.add(tabbedPaneEvents);

		JPanel panel = new JPanel();
		tabbedPaneEvents.addTab("Today's Events", null, panel, null);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		textAreaTodaysEvents = new JTextArea();
		panel.add(textAreaTodaysEvents);

		JPanel panel_2 = new JPanel();
		tabbedPaneEvents.addTab("Upcoming Events", null, panel_2, null);
		panel_2.setLayout(new GridLayout(0, 1, 0, 0));

		textAreaUpcomingEvents = new JTextArea();
		panel_2.add(textAreaUpcomingEvents);

		JPanel panel_3 = new JPanel();
		tabbedPaneEvents.addTab("Recent Events", null, panel_3, null);
		panel_3.setLayout(new GridLayout(1, 0, 0, 0));

		textAreaRecentEvents = new JTextArea();
		panel_3.add(textAreaRecentEvents);

		JButton btnBack = new JButton("Back");
		tabbedPaneEvents.addTab("Back", null, btnBack, null);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				back();
			}
		});
		btnBack.setBackground(Color.YELLOW);
	}

	/**
	 * Updates all event information textAreas.
	 */
	public void updateEvents()
	{
		System.out.println("Events updated");
		EventCtr eventCtr = EventCtr.getInstance();
		ArrayList<Event> currentEvents = eventCtr.getEvents("TodaysEvents");
		ArrayList<Event> upcomingEvents = eventCtr.getEvents("UpcomingEvents");
		ArrayList<Event> recentEvents = eventCtr.getEvents("RecentEvents");

		textAreaTodaysEvents.setText("");
		textAreaUpcomingEvents.setText("");
		textAreaRecentEvents.setText("");

		for(Event event : currentEvents)
		{
			textAreaTodaysEvents.insert(
					"Description: "+event.getDescription()+
					" Date: "+event.getDate()+
					" StartTime: "+event.getStartTime()+
					" EndTime: "+event.getEndTime()+
					" Pricetype: "+event.getPriceType()+"\n", 0 );
		}

		for(Event event : upcomingEvents)
		{
			textAreaUpcomingEvents.insert(
					"Description: "+event.getDescription()+
					" Date: "+event.getDate()+
					" StartTime: "+event.getStartTime()+
					" EndTime: "+event.getEndTime()+
					" Pricetype: "+event.getPriceType()+"\n", 0 );
		}

		for(Event event : recentEvents)
		{
			textAreaRecentEvents.insert(
					"Description: "+event.getDescription()+
					" Date: "+event.getDate()+
					" StartTime: "+event.getStartTime()+
					" EndTime: "+event.getEndTime()+
					" Pricetype: "+event.getPriceType()+"\n", 0 );
		}

		textAreaTodaysEvents.insert("\n",0);
		textAreaUpcomingEvents.insert("\n",0);
		textAreaRecentEvents.insert("\n",0);
		textAreaTodaysEvents.insert("Today's Events:",0);
		textAreaUpcomingEvents.insert("UpcomingEvents:",0);
		textAreaRecentEvents.insert("RecentEvents:",0);
	}

	/**
	 * @return the tabbedPaneEvents
	 */
	public JTabbedPane getTabbedPaneEvents() {
		return tabbedPaneEvents;
	}

	/**
	 * returns the user to his mainGUI
	 */
	private void back()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getEventGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getMainGUIPanels().get(employee.getLogin()));
			MainFrame.getMainGUIPanels().get(employee.getLogin()).repaint();
		} catch (Exception e) {
			System.err.println("return to mainGUI failed");
			e.printStackTrace();
		}
	}
}
